<?php
//OPTIMIZEME : ce code doit être optimisié
class RolesManagerByRole 
{
	protected $_data;
	
	public function RolesManagerByRole($rolename,$modulenamefilter,$request)
    {        	
        $aclrights = new Wmp_Model_AclrightsTable();
        
    	if($request->getPost()) {
    		$request = $request->getPost();
    		$privileges = $request['privilege'];
    		
    		if(count($privileges)>0)
    		foreach ( $privileges as $actionid => $privilege)    		
    			$aclrights->setPrivilege($actionid,$rolename,$privilege);    		
        }
        //$privileges = $request['privilege'];
    		
    	//get actions privileges for the given role name
    	
    	$roles = $aclrights->getActionsByRole($rolename,$modulenamefilter);
    	
      	$translate =  zend_registry::get('Zend_Translate');
		
    	$modulesList = new Wmp_Model_ModulesTable();
    	$modulesList = $modulesList->fetchAll();
    	
    	$rolesList = new Wmp_Model_AclRolesTable();
    	$rolesList = $rolesList->fetchAll();
    	
    	$_data = "<form method='post' action='' >";
    	$_data .="<table><tr>";
    	$_data .="<td>Module ";//FIXME : $this->translate.....
    	$_data .="<select name='modname' onChange='submit()'>";
    	$_data .="<option value='all'>Filter by Module</option>";//FIXME : $this->translate.....
    	
    	foreach($modulesList as $currentmodule) {
    		if($currentmodule->name==$modulenamefilter)
				$_data .='<option value="'.$currentmodule->name.'" SELECTED >'.$currentmodule->name.'</option>';
    		else
    			$_data .='<option value="'.$currentmodule->name.'" >'.$currentmodule->name.'</option>';
    	}
    	
    	$_data .='</select>';
    	$_data .="</td>";
    	$_data .="<td>Role ";//FIXME : $this->translate.....
    	$_data .="<select name='role' onChange='submit()'>";
    	
    	$_data .="<option value='all'>Select a role</option>";//FIXME : $this->translate.....
    	
    	foreach($rolesList as $currentrole) {
    		if($currentrole->name==$rolename)
				$_data .='<option value="'.$currentrole->name.'" SELECTED >'.$currentrole->name.'</option>';
    		else
    			$_data .='<option value="'.$currentrole->name.'" >'.$currentrole->name.'</option>';
    	}
    	
    	$_data .='</select>';
    	$_data .="</td>";
    	$_data .="</tr></table>";
    	$_data .= "</form>";
    	
    	//check if $rolename and module exist
    	$isRolename = new Wmp_Model_AclRolesTable();
    	$isRolename = $isRolename->find($rolename)->current();
    	if(!$isRolename) {
    		$this->_data = $_data;
    		return;
    	}
    	
    	//check if $modulenamefilter exist
    	if($modulenamefilter!='all') {
	    	$isModule = new Wmp_Model_ModulesTable();
	    	$isModule = $isModule->find($modulenamefilter)->current();
	    	if(!$isModule) {
	    		$this->_data = $_data;
	    		return;
	    	}
    	}
    	
    	//OPTIMIZEME:trouver un meilleur moyen de recuperer l'object view
		$viewRenderer =  Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$view = $viewRenderer->view;
    	$urlUpdate = $view->url( array(	'language'=>zend_registry::get('Zend_Locale'),
    						'module'=>'admin',
    						'controller'=>'system-access',
    						'action'=>'access-manager') ); 
 
    	$_data .="<form method='post' action='".$urlUpdate."' >"; //URGENT à corriger par php self
    	$_data .="<input type='hidden' name='role' value='".$rolename."' />";
    	$_data .="<input type='hidden' name='modname' value='".$modulenamefilter."' />";         
    	$_data .="<table width='100%' border='1'>";
    	$_data .='<tr>
    				<th>'.$translate->_('module-name').'</th>
    				<th>'.$translate->_('action-num').'</th>
    				<th>'.$translate->_('action-name').'</th>
    				<th>'.$translate->_('action-description').'</th>
    				<th>'.$translate->_('allow-privilege').'</th>
    				<th>'.$translate->_('deny-privilege').'</th>
    				<th>'.$translate->_('inherits-privilege').'</th>
    			</tr>';    	      
       
    	$lastmodule="";
        foreach($roles as $key => $role) {
        	
           	if($role['privilege']=="allow")
        		$allowIsChecked="checked checked";
        	else if($role['privilege']=="deny")
        		$denyIsChecked="checked checked";
        	else
        		$inheritIsChecked="checked checked";
	        	
        	$_data .='<tr>';
        	$_data .='<td>'. $role['module'].'</td>';
        	$_data .='<td>'. $role['actionid'].'</td>';
        	$_data .='<td>'. $role['action'].'</td>';
        	$_data .='<td>'. $role['description'].'</td>';//FIXME : utilister plutot WmpTranslate (pour le multilang)
        	$_data .='<td><input type="radio" name="privilege['.$role['actionid'].']" value="allow" '.@$allowIsChecked.' /></td>';
        	$_data .='<td><input type="radio" name="privilege['.$role['actionid'].']" value="deny" '.@$denyIsChecked.' /></td>';
        	$_data .='<td><input type="radio" name="privilege['.$role['actionid'].']" value="parent" '.@$inheritIsChecked.' /></td>';
        	$_data .='</tr>';
        	
        	$allowIsChecked="";
        	$denyIsChecked="";
        	$inheritIsChecked="";
        }

        $_data .='</table>';
        $_data .='<input type="submit" value="'.$translate->_('validate').'" >';
        $_data .='</form>';

        $this->_data = $_data;
    }
    
    public function getData()
    {
    	return $this->_data;
    }
}